#include "CSprite.h"


CSprite::CSprite()
{
	m_nSpriteWidth			= 0;
	m_nSpriteHeight			= 0;
	m_nNumFrame				= 1;
	m_nNumFrameInRow		= 1;
	m_nCurrentAnimation		= 0;

	m_XScaleRatio			= 1.0f;
	m_YScaleRatio			= 1.0f;
	m_fAngle				= 0.0f;
	m_Color					= D3DCOLOR_ARGB(255,255,255,255);

	m_vecRotateCenter		= D3DXVECTOR2(0,0);
	m_vecScaleCenter		= D3DXVECTOR2(0,0);

	m_pAnimation			= new CAnimation[1];
	m_pAnimation->CreateAnimation( 0 , 0 , m_nNumFrame - 1 , 0 , 1.0f/60.0f );
	ZeroMemory( &m_scrRect , sizeof(RECT) ); 
}

CSprite::CSprite( int NumFrame , int NumFrameInRow , int width , int height )
{
	m_nSpriteWidth			= width;
	m_nSpriteHeight			= height;
	m_nNumFrame				= NumFrame;
	m_nNumFrameInRow		= NumFrameInRow;
	m_nCurrentAnimation		= 0;

	m_XScaleRatio			= 1.0f;
	m_YScaleRatio			= 1.0f;
	m_fAngle				= 0.0f;
	m_Color					= D3DCOLOR_ARGB(255,255,255,255);

	m_vecRotateCenter		= D3DXVECTOR2(0,0);
	m_vecScaleCenter		= D3DXVECTOR2(0,0);

	m_pAnimation			= new CAnimation[1];
	m_pAnimation->CreateAnimation( 0 , 0 , m_nNumFrame - 1 , 0 , 1.0f/60.0f );
	ZeroMemory( &m_scrRect , sizeof(RECT) ); 
}


CSprite::~CSprite()
{
	Release();
}


//-----------------------------------------//
// Name : Release                          //
// Desc : Releasing object , memory etc... //
//-----------------------------------------//
void CSprite::Release()
{
	if(m_pAnimation)
		delete []m_pAnimation;

	m_pAnimation		= NULL;
}





//----------------------------------------//
// Name : Update						  //
// Desc : Update the frame of this sprite //
//----------------------------------------//
void CSprite::Update( float deltaTime )
{
	m_pAnimation[m_nCurrentAnimation].UpdateAnimation(deltaTime , &m_scrRect , m_nSpriteWidth , m_nSpriteHeight , m_nNumFrameInRow );
}





//-------------------------------------------------//
// Name : SetAnimation                             //
// Desc : Set animation infomation for this sprite //
//-------------------------------------------------//
void CSprite::SetAnimation(CAnimation *Animation)
{
	if(!Animation)
		return;

	if(m_pAnimation )
		Release();

	m_pAnimation = Animation;
}



//--------------------------------------------------------//
// Name : SetCurrAnimation                                //
// Desc : Set the current animation index for this sprite // 
//--------------------------------------------------------//
void CSprite::SetAnimationIndex(ULONG CurrAnimationIndex)
{
	if( m_nCurrentAnimation == CurrAnimationIndex )
		return;
	m_nCurrentAnimation = CurrAnimationIndex;
	m_pAnimation[m_nCurrentAnimation].SetCurrentFrameIndex(m_pAnimation[m_nCurrentAnimation].GetStartFrameIndex());
	m_scrRect = m_pAnimation[m_nCurrentAnimation].UpdateSourceRect( m_nSpriteWidth , m_nSpriteHeight , m_nNumFrameInRow );
}


//----------------------------------------------------//
// Name : SetScale                                    //
// Desc : Set the X and Y scale ratio for this sprite //
//----------------------------------------------------//
void CSprite::SetScale(float xScaleRatio, float yScaleRatio , D3DXVECTOR2 vecScaleCenter /* = D3DXVECTOR2(0,0) */ )
{
	m_XScaleRatio = xScaleRatio;
	m_YScaleRatio = yScaleRatio;
	m_vecScaleCenter = vecScaleCenter;
}




//---------------------------------------------------------------//
// Name : SetColor												 //
// Desc : Setting the color and the alpha chanel for this sprite //
//---------------------------------------------------------------//
void CSprite::SetColorAlpha(int a, int r /* = 255 */ , int g /* = 255 */ , int b /* = 255 */)
{
	m_Color = D3DCOLOR_ARGB( a , r , g , b );
}



//--------------------------------------//
// Name : SetAngle                      //
// Desc : Set the angle for this sprite //
//--------------------------------------//
void CSprite::SetAngle(float angle , D3DXVECTOR2 vecRotateCenter /* = D3DXVECTOR2(0,0) */)
{
	m_fAngle = angle;
	m_vecRotateCenter = vecRotateCenter;
}





//---------------------------------------------------------//
// Name : GetCurrAnimation                                 //
// Desc : Get the current animation this sprite is playing //
//---------------------------------------------------------//
ULONG CSprite::GetCurrAnimationIndex()
{
	return m_nCurrentAnimation;
}



//-----------------------------------------------------//
// Name : GetAnimation                                 //
// Desc : Get the animation of this sprite             //
//			-1 mean get all the animation              //
//			other index will get this index animation  //
//-----------------------------------------------------//
CAnimation* CSprite::GetAnimation(int index /*= -1*/)
{
	if(index < 0 )
		return m_pAnimation;
	else
		return &m_pAnimation[index];
}



//---------------------------------//
// Name : GetXScaleRatio           //
// Desc : Get the X scaling factor //
//---------------------------------//
float CSprite::GetXScaleRatio()
{
	return m_XScaleRatio;
}


//---------------------------------//
// Name : GetYScaleRatio           //
// Desc : Get the Y scaling factor //
//---------------------------------//
float CSprite::GetYScaleRatio()
{
	return m_YScaleRatio;
}



//---------------------------------------------//
// Name : GetAngle                             //
// Desc : Get the current angle of this sprite //
//---------------------------------------------//
float CSprite::GetAngle()
{
	return m_fAngle;
}


//----------------------------------------------------------//
// Name : GetColor											//
// Desc : Get the color and the alpha chanel of this sprite //
//----------------------------------------------------------//
D3DCOLOR CSprite::GetColor()
{
	return m_Color;
}




//----------------------------------------------------//
// Name : GetVecRotateCenter                          //
// Desc : Get the vector rotate center of this sprite //
//----------------------------------------------------//
D3DXVECTOR2 CSprite::GetVecRotateCenter()
{
	return m_vecRotateCenter;
}


//----------------------------------------------------//
// Name : GetVecScaleCenter                          //
// Desc : Get the vector rotate center of this sprite //
//----------------------------------------------------//
D3DXVECTOR2 CSprite::GetVecScaleCenter()
{
	return m_vecScaleCenter;
}



//-----------------------------------------------------------------//
// Name : GetSourceRect											   //
// Desc : Get the current source rect of this sprite for rendering //
//-----------------------------------------------------------------//
RECT CSprite::GetSourceRect()
{
	return m_scrRect;
}




